package com.dong.com.love.cach;

import android.content.Context;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Dong on 2016/11/2.
 */

public abstract class CachCallBack<T> implements Callback<T> {

    private  Context context;
    private String name;
    private  T t;

    protected CachCallBack(Context context, String name) {
        this.context = context;
        this.name = name;
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        t = response.body();

        if(t==null)
            return;

        saveCach();
        update(t);
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {

    }

    /*
      存入缓存
     */
    private  void saveCach(){
        CachUtils.saveCach( context, t, name);
    }
    /*
      提醒更新
     */
    public abstract void update(T t);
}
